<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Paper extends Admin_Controller {

    function __construct(){
        parent::__construct();
        $this->admin_navigation->parent_active('paper');
        $this->load->library('session');
        $this->output->enable_profiler(TRUE);
        $this->load->model('paper/paper_model');
    }

    function manager_paper() {
        $columns = array(
                array(
                    'name' => 'ID',
                    'type' => 'id',
                    'index' => 'new_id',
                    'sort_column' => true,
                ),
                array(
                        'name' => 'Tên',
                        'type' => 'text',
                        'filter' => 'name',
                        'index' => 'name',
                        'sort_column' => true,
                ),
                array(
                        'name' => 'Bài hot',
                        'type' => 'publish',
                        'filter' => 'is_hot',
                        'index' => 'is_hot',
                        'options' => array('0' => 'Không', '1' => 'Có')
                ),
                array(
                        'name' => __('Category'),
                        'type' => 'select',
                        'filter' => 'category_id',
                        'index' => 'category_name',
                        'options' => $this->paper_model->get_categories_options()
                ),
                array(
                        'name' => 'Ngày tạo',
                        'type' => 'date',
                        'index' => 'created',
                        'sort_column' => true,
                ),
                array(
                        'name' => 'Đặc sắc',
                        'type' => 'publish',
                        'filter' => 'featured',
                        'index' => 'featured',
                        'options' => array('0' => 'Không', '1' => 'Có')
                ),
                array(
                        'name' => 'Trạng thái',
                        'type' => 'publish',
                        'filter' => 'status',
                        'index' => 'status',
                        'options' => array('0' => 'Tắt', '1' => 'Bật')
                ),
            );
        $this->grid->columns($columns);
        $this->grid->datasource('paper/paper_model','get_papers');
        $this->grid->set_title('Paper Manager');
        $this->grid->add_button('Thêm mới',admin_url('*/paper/add_paper'),'add');
        $this->grid->set_row_action('edit_paper');
        $this->grid->total_rows($this->paper_model->get_count());
        $this->grid->add_mass_action('delete', array(
                'label'=>'Xóa',
                'url'      => admin_url('*/paper/delete_paper'),
                'confirm'  => 'Are you sure?'
        ));
        $this->grid->add_mass_action('status', array(
                'label'=>'Thay đổi trạng thái',
                'url'      => admin_url('*/paper/change_status_paper'),
        ));
        if ($this->input->is_ajax()){
            $this->output->set_output($this->grid->display());
        }else{
            $this->load->view('admin/grid');
        }
    }

    function edit_paper() {
        $paper_id = $this->input->get_param('id', false);
        $this->load->model('paper/paper_model');
        if ($paper_id) {
            $data = $this->paper_model->get_paper($paper_id);
            $this->form->set_values($data);
        }
        $this->form->set_form_attributes(array(
                'id' => 'paper_form',
                'action' => admin_url('*/paper/edit_post_paper' , array('id' => $paper_id)),
                'enctype' => 'multipart/form-data',
                ));
        $this->form->add_filedset('ganeral', array('legend' => 'General'));
        $this->form->add_filed('name', 'text', array('label' => 'Tiêu đề', 'name' => 'name', 'required' => true,));
        $this->form->add_filed('alias', 'alias', array('label' => $this->__('Alias'), 'name' => 'alias',));
        $this->form->add_filed('cat_id', 'select', array('label' => 'Danh mục', 'name' => 'category_id', 
                'values' => $this->paper_model->get_categories_options(), 'required' => true,));
        $this->form->add_filed('image', 'image', array('label' => 'Hình ảnh', 'name' => 'image',));
        $this->form->add_filed('status', 'select', array('label' => 'Trạng thái', 'name' => 'status', 'values' => array('Tắt', 'Bật')));
        $this->form->add_filed('featured', 'select', array('label' => 'Đặc sắc', 'name' => 'featured', 'values' => array('Không', 'Có')));
        $this->form->add_filed('is_hot', 'select', array('label' => 'Nổi bật', 'name' => 'is_hot', 'values' => array('Không', 'Có')));
        $this->form->add_filed('introtext', 'textarea', array('label' => 'Tóm tắt', 'name' => 'introtext',));
        $this->form->add_filed('fulltext', 'editor', array('label' => 'Nội dung', 'name' => 'fulltext',));
        $this->load->view('admin/form', array('form' => $this->form->to_html()));
    }

    function add_paper() {
        $this->_forward('edit_paper');
    }

    function delete_paper() {
        $cids = explode(',', $this->input->get_param('new_id',false));
        $this->load->model('paper/paper_model');
        foreach ($cids as $cid) {
            $this->paper_model->delete_paper($cid);
        }
        $this->_redirect(admin_url('*/paper/manager_paper'));
    }

    function change_status_paper() {
        $cids = explode(',', $this->input->get_param('new_id',false));
        $this->load->model('paper/paper_model');
        foreach ($cids as $cid) {
            $this->paper_model->change_status_paper($cid);
        }
        $this->_redirect(admin_url('*/paper/manager_paper'));
    }

    function edit_post_paper() {
        $paper_id = $this->input->get_param('id', false);
        $this->load->model('paper/paper_model');
        if (($data = $this->input->post())) {
            $config['upload_path'] = './images/content/';
            $config['allowed_types'] = 'gif|jpg|png';
            $this->load->library('upload', $config);

            if ($this->upload->do_upload('image')) {
                $data['image'] = 'images/content/' . $this->upload->get_file_name();
            }
            if ($paper_id) {
                $this->paper_model->update_paper($data, $paper_id);
            } else {
                $this->paper_model->insert_paper($data);
            }
            $this->_redirect(admin_url('*/paper/manager_paper'));
        }
    }
    
    function manager_paper_category() {
        $columns = array(
                array(
                        'name' => 'ID',
                        'type' => 'id',
                        'index' => 'category_id',
                ),
                array(
                        'name' => 'Tên',
                        'type' => 'text',
                        'index' => 'treename',
                        
                ),
                array(
                        'name' => 'Ngày tạo',
                        'type' => 'date',
                        'index' => 'created',
                ),
                array(
                        'name' => 'Trạng thái',
                        'type' => 'publish',
                        'index' => 'status',
                        'options' => array('0' => 'Tắt', '1' => 'Bật')
                ),
        );
        $this->grid->columns($columns);
        $this->grid->datasource('paper/paper_model','get_categories');
        $this->grid->set_title('Category Manager');
        $this->grid->add_button('Thêm mới',admin_url('*/paper/add_paper_category'),'add');
        $this->grid->set_row_action('edit_paper_category');
        $this->grid->total_rows($this->paper_model->get_count_category());
        $this->grid->add_mass_action('delete', array(
                'label'=>'Xóa',
                'url'      => admin_url('*/paper/delete_paper'),
                'confirm'  => 'Are you sure?'
        ));
        if ($this->input->is_ajax()){
            $this->output->set_output($this->grid->display());
        }else{
            $this->load->view('admin/grid');
        }
    }

    function edit_paper_category() {
        ;
    }

    function add_paper_category() {
        ;
    }

    function delete_paper_category() {
        ;
    }

    function edit_post_paper_category() {
        ;
    }
    function publish_category() {
        
    }
}
?>